package fr.upmf.ic2a.sharedata.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.example.ProjetAndroid.R;


public class PreferencesUserActivity extends Activity implements android.view.View.OnClickListener, OnSeekBarChangeListener {
	
	public static final String PREFS_NAME = "DataSharePreferences";

	private EditText email;
	private EditText password;
	private EditText adresseServeur;
	private SeekBar synchr;
	private TextView valueS;
	private Button modifier;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preference_user);
                
		// on recupere les preferences
        
		SharedPreferences preferences =  PreferenceManager
				.getDefaultSharedPreferences(this);
        
		String adresseMail = preferences.getString("login", null);
		String mdp = preferences.getString("password", null);
		String serveur = preferences.getString("serveur", null);
		String s = preferences.getString("delaiSynch", null);
        int synch = Integer.parseInt(s);
		System.out.println("@@@@@@" + adresseMail + mdp + serveur);
        
        email = (EditText)findViewById(R.id.editText1);
        email.setText(adresseMail);
        
        password = (EditText)findViewById(R.id.editText2);
        password.setText(mdp);
        
        adresseServeur = (EditText)findViewById(R.id.editText3);
        adresseServeur.setText(serveur);
        
        synchr = (SeekBar)findViewById(R.id.seekBar1);
        synchr.setProgress(synch);   
        synchr.setOnSeekBarChangeListener(this);
        
        modifier = (Button)findViewById(R.id.bModifier);
        modifier.setOnClickListener(this);
        
        valueS = (TextView)findViewById(R.id.textView6);
        valueS.setText(s);
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_received_share, menu);
        return true;
    }
    
    
    public void onClick(View v) {

		if (v.getId() == R.id.bModifier) {
			
			String login = email.getText().toString();
			String mdp = password.getText().toString();
			String serveur = adresseServeur.getText().toString();
			int syn = synchr.getProgress();
			String s = String.valueOf(syn);
			
			SharedPreferences preferences = PreferenceManager
					.getDefaultSharedPreferences(this);
			SharedPreferences.Editor prefsEditor = preferences.edit();
			prefsEditor.putString("login", login);
			prefsEditor.putString("password", mdp);
			prefsEditor.putString("serveur",serveur);
			prefsEditor.putString("delaiSynch", s);
			prefsEditor.commit();
			
			Intent intent = new Intent(PreferencesUserActivity.this,
					AccueilUserActivity.class);
			startActivity(intent);
		
		
		}
    }
    
    public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser){
    	valueS = (TextView)findViewById(R.id.textView6);
    	
    	valueS.setText(String.valueOf(seekBar.getProgress()));
    }

	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
	}

}
